<?php
session_start();

include_once '../pushing.php';

if ($pushTime == true) {
	if ($_SESSION['UserId'] != 3)
		header('Location: push.php');
}

if(!isset($_SESSION['UserId'])) {
	header('Location: login.php');
	die();
}

require_once '../mysqlConnect.php';
require_once '../Model/Icd_Code.php';
require_once '../Model/Icd_Nine_Entry.php';

$ValidationErrors = null;

if (isset($_POST['date']) && isset($_POST['icd_name'])) {
	
	$icn_nine_entry = new Icd_Nine_Entry();
	$icn_nine_entry->set(array('patient_id' => $_GET['PatId'], 'code_id' => $_POST['icd_name'], 'date_given' => $_POST['date']));

	$save = $icn_nine_entry->save();
	if ($save === true) {
		echo '<script language="javascript" type="text/javascript">
		
			function redirect(){
				window.location.href = "Add_ICD9.php?PatId=' . $_GET['PatId'] . '";
			}
			
			alert("Successfully Added ICD-9 Entry to Database!");
			redirect();
		
		</script>';
	} else
		$ValidationErrors = $save;
}

function retrieveAllICDObjects() {
		
		$DBH = GetPDOConnection();
		$sql = "SELECT * FROM icd_codes ORDER BY code";
		
		try {
			$STH = $DBH->prepare($sql);
			$STH->setFetchMode(PDO::FETCH_CLASS, "Icd_Code");
			$STH->execute();
			$result = $STH->fetchAll();
			return $result;
		} catch (PDOException $e) {
			$ValidationErrors = "Can't query database for ICD9 Codes: " . $e->getMessage();
		}
}

function getPatientInfobyID($id)
{
	$conn = GetConnection();
	$sql = "SELECT * FROM Patient WHERE ID ='$id'";
	$value = $conn->query($sql);
	$rs = $value->fetch_assoc();
	return $rs;	
	$conn->close();
}

$patientInfo = getPatientInfobyID($_GET['PatId']);

$icd_codes = retrieveAllICDObjects();
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Add ICD9</title>
		<script src="../JavaScript/jQueryTabs/js/jquery-1.6.2.min.js" type="text/javascript"></script>
		<script src="../JavaScript/jQueryTabs/js/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
		<link rel="stylesheet" type="text/css" href="../JavaScript/jQueryTabs/css/custom-theme/jquery-ui-1.8.16.custom.css"/>
		<link rel="stylesheet" type="text/css" href="../Styler.css" />
		<script type="text/javascript">
		$(document).ready(function() {
			
			$("#date").datepicker({ minDate: new Date(1950, 01, 01), maxDate: new Date(), showOn: 'both', dateFormat: "yy-mm-dd"});
			
		});
	</script>
	</head>

	<body>
	
	<div id="header">
    	<div class="pageTitleText">Add ICD9</div>
    	<div class="pageTitleText" style="font-size: 1em;">
	        <span class="pageTitleSub">Patient: <?php echo $patientInfo['PatID'];?></span>
	        <span class="pageTitleSub">Date Diagnosed: <?php echo $patientInfo['DateDiagnosed'];?></span>
        </div>
 	</div>
   	 
    <div id="mainMenu" style="height: 451px;">
    	<ul style="list-style: none outside none;">
    	
    		<li>
    			<a href="choosePatient.php?SID=10" style="font-weight: bold; color: #7C3709;">Choose New Patient</a>
    		</li>
    		
    		<li>
	         	<a href="Add_ICD9.php?PatId=<?php echo $_GET['PatId'];?>" style="font-weight: bold; color: #FB7D08;">Patient ICD9</a>
	        </li>
    	
    		<li>
	         	<a href="Add Prescription.php?PatId=<?php echo $_GET['PatId'];?>">Patient Prescription</a>
	        </li>
	        
	        <li>
	        	<a href="Physical Symptoms.php?PatId=<?php echo $_GET['PatId'];?>">Physical Symptoms</a>
	        </li>
	        
	        <li>
	        	<a href="Neuropsychological Symptoms.php?PatId=<?php echo $_GET['PatId'];?>">Neuropsychological Symptoms</a>
	        </li>
	        
	        <li>
	        	<a href="Psychological Test.php?PatId=<?php echo $_GET['PatId'];?>">Psychological Test</a> 
	        </li>
	        
	        <li>
	        	<a href="Diagnostic Test.php?PatId=<?php echo $_GET['PatId'];?>">Diagnostic Test</a>
	        </li>
	        
	        <li>
	        	<a href="viewPatient.php?PatId=<?php echo $_GET['PatId'];?>">View Patient Information</a>
	        </li>
	        
	        <li>
	         	<a href="Edit Patient.php?PatId=<?php echo $_GET['PatId'];?>">Edit Patient Information</a>
	        </li>
	        
	        <li>
	        	<a href="Save Information.php?PatId=<?php echo $_GET['PatId'];?>">Save Patient Information</a>
	        </li>
	        
	        <li style="border-bottom: 1px solid #FD9D45;">
	       	 	<del><!--<a href="query.php">-->Query<!--</a>--></del>
	        </li>
    	
	        <li>
	       	 	<a href="home.php">Home</a>
	        </li> 
	         
	        <li>
	       	 	<a href="logout.php">Logout</a>
	        </li>
	        
        </ul>
	</div>
     
     <?php if(isset($ValidationErrors)) {?>
			
			<div class="noQueryResults">
			    <img src="../Images/error.png" style="float:left; margin: 5px 5px 5px 15px;"></img>
			    <p style="font-weight: bold; line-height: 1; margin: 5px 5px 5px 55px;">Error</p>
				<p style="line-height: 1; margin: 5px 5px 5px 55px;">		
		    	<?php echo $ValidationErrors;?>
		    	</p>
			</div>
			
    <?php } ?>
     
     
     
	<div class="formContainer">
		<div id="login" style="top: 10px; margin: 0px; width: 600px; height: 200px;"> 
		
	        <form name="addICD9Entry" method="post">
	             
				<div style="border-bottom: 1px solid #CCC; padding-bottom: 10px;">    
	                
	                <p>
	                	<div style="width: 80px; float:left;">
	                		<label for="icd_name">ICD Code</label>
	                	</div>
		                <select id="icd_id" name="icd_name" style="width: 450px;">
		                <?php foreach ($icd_codes as $codeObject) {
		                	
		                	$codes = $codeObject->get();
		                	
		                	if (isset($_POST['icd_name']) && $_POST['icd_name'] == $codes['id'])
		                		echo '<option value="' . $codes['id'] . '" selected="selected">';
		                	else
		                		echo '<option value="' . $codes['id'] . '">';
		                	
		                	echo $codes['code'] . " : " . $codes['name'];
		                	echo '</option>';
		                }?>
		                </select>
	                </p>
	                
	               <p>
		               <div style="width: 80px; float:left;">
		               		<label for="date" >Date</label>
		               </div>
		               <input type="text" id="date" name="date" style="width: 450px;" value="<?php if (isset($_POST['date'])) echo $_POST['date'];?>"></input>
	                </p>
				</div>
	         	
            	<div id="QuesButton">
            	 	<input type="submit" value="Submit" class="loginButton" style="margin-top: 0px;"/>
		        </div>    	
	        	
	        </form>
	        
		</div>
	</div>
	</body>
</html>
